package buaa.com.readview;

import android.os.Environment;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import buaa.com.util.FileUtils;
import buaa.com.util.Globals;
import buaa.com.util.PageDao;
import buaa.com.util.TxtDao;
import buaa.com.view.MyView;

public class MainActivity extends AppCompatActivity {
    //文本信息
    private Map<String,Object> txtMap;
    //文本详细内容
    private Map<String,Object> pageMap = new HashMap<>();

    private StringBuilder sb = new StringBuilder();
    //当前读到的行数
    private int lineCount;
    //总页数
    private int pageNum = 1;
    //当前页数
    private int nowPage = 1;
    private String fullPath;

    private MyView contentView;
    private TextView pageView;

    private Handler handler;
    private List<String> txtList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Globals.init(this);
        setContentView(R.layout.activity_main);
        contentView =(MyView)findViewById(R.id.content_view);
        pageView = (TextView)findViewById(R.id.page_view);
        txtList = FileUtils.readAll(Environment.getExternalStorageDirectory().getAbsolutePath(),".txt");
        handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                if(msg.what == 0){
                    pageView.setText("正在分页，已经分解"+(pageNum-1)+"页");
                }else{
                    pageView.setText("分页结束"+nowPage+"/"+pageNum);
                }
            }
        };
        Log.d("fullPath","......................."+Environment.getExternalStorageDirectory().getAbsolutePath());
        fullPath = txtList.get(0);

        TxtDao.insertTxtData(fullPath);
        txtMap = TxtDao.findTxtByFullPath(fullPath);
        //需要分页
        if((int)txtMap.get("overFlag")==0){
            Thread t = new Thread(){
                @Override
                public void run() {
                    try {
                        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(fullPath),"GBK"));
                        String line = null;
                        while ((line = br.readLine())!=null){
                            while (line.length() > Globals.LINE_CHAR_COUNT) {
                                String str = line.substring(0, Globals.LINE_CHAR_COUNT);
                                line = line.substring(Globals.LINE_CHAR_COUNT);
                                addLine(str);
                            }
                            addLine(line);
                        }
                        br.close();
                        handler.sendEmptyMessage(1);
                        TxtDao.updateTxtOverFlag(fullPath);

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            };
            t.start();
        }else{
            //不需要分页 直接返回上次的页面
            pageNum = PageDao.getPageCount((int)txtMap.get("txtid"));
            nowPage = (int)txtMap.get("nowPage");
            contentView.changeData();
        }


    }

    public void addLine(String str){
        sb.append(str);
        sb.append(Globals.END_FLAG);
        lineCount++;
        if(lineCount==Globals.LINE_COUNT){
            pageMap.put("txtid", txtMap.get("txtid"));
            pageMap.put("pagenum", pageNum++);
            pageMap.put("content", sb.toString());
            PageDao.insertPageData(pageMap);
            if(pageNum==10){
                contentView.changeData();
            }
            handler.sendEmptyMessage(0);
            sb = new StringBuilder();
            lineCount = 0;
        }
    }

    @Override
    protected void onDestroy() {
        TxtDao.updateNowPage(nowPage,fullPath);
        super.onDestroy();

    }
    public Map<String, Object> getTxtMap() {
        return txtMap;
    }

    public void setTxtMap(Map<String, Object> txtMap) {
        this.txtMap = txtMap;
    }

    public int getPagenum() {
        return pageNum;
    }

    public void setPagenum(int pagenum) {
        this.pageNum = pagenum;
    }

    public int getNowPage() {
        return nowPage;
    }

    public void setNowPage(int nowPage) {
        this.nowPage = nowPage;
    }

    public Handler getHandler() {
        return handler;
    }

    public void setHandler(Handler handler) {
        this.handler = handler;
    }
}
